package com.cskaoyan.service;

import com.cskaoyan.bean.goods.Bo.*;
import com.cskaoyan.bean.goods.Bo.goodsCreate.*;
import com.cskaoyan.bean.goods.Bo.goodsUpdate.GoodsUpdateBo;
import com.cskaoyan.bean.goods.Bo.goodsUpdate.GoodsUpdateOfAtrBo;
import com.cskaoyan.bean.goods.Bo.goodsUpdate.GoodsUpdateOfGoodsBo;
import com.cskaoyan.bean.goods.vo.GoodsListFromTopicVO;
import com.cskaoyan.bean.goods.vo.catVo.*;
import com.cskaoyan.bean.goods.vo.detail.*;
import com.cskaoyan.bean.goods.vo.goods.GoodsListVo;
import com.cskaoyan.bean.goods.vo.goods.GoodsListVoData;
import com.cskaoyan.bean.goods.vo.goods.GoodsListVoOfGoods;
import com.cskaoyan.bean.goods.vo.goods.GoodsListVoOfGoodsOfUrl;
import com.cskaoyan.bean.po.*;
import com.cskaoyan.bean.wx.topic.Vo.WxTopicListOfListVo;
import com.cskaoyan.mapper.*;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.Constant;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jdk.nashorn.internal.ir.ContinueNode;
import org.aspectj.weaver.ast.Var;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @Author: 蒋镇泽
 * @Description:
 * @Date: 2022-06-05-15:51
 */
@Service
@Transactional
public class GoodsServiceImpl implements GoodsService {
    @Autowired
    MarketGoodsMapper marketGoodsMapper;
    @Autowired
    MarketCategoryMapper marketCategoryMapper;
    @Autowired
    MarketBrandMapper marketBrandMapper;

    @Autowired
    MarketGoodsSpecificationMapper marketGoodsSpecificationMapper;
    @Autowired
    MarketGoodsProductMapper marketGoodsProductMapper;
    @Autowired
    MarketGoodsAttributeMapper marketGoodsAttributeMapper;
    @Autowired
    ObjectMapper objectMapper;

    @Override
    public GoodsListVo query(BaseParamBo baseParam) {
        MarketGoodsExample example = new MarketGoodsExample();
        MarketGoodsExample.Criteria criteria = example.createCriteria();
        if (baseParam.getGoodsId() != null && !baseParam.getGoodsId().equals("")) {
            criteria.andIdEqualTo(baseParam.getGoodsId());
        }
        if (baseParam.getGoodsSn() != null && !baseParam.getGoodsSn().equals("")) {
            criteria.andGoodsSnEqualTo(baseParam.getGoodsSn());
        }

        if (baseParam.getName() != null && !baseParam.getName().equals("")) {
            criteria.andNameLike("%" + baseParam.getName() + "%");
        }
        criteria.andDeletedEqualTo(false);
        example.setOrderByClause("add_time desc");

        List<MarketGoods> marketGoods = marketGoodsMapper.selectByExample(example);
        GoodsListVoData goodsListVoData = new GoodsListVoData();

        //判断是分页还是怎样
        if (baseParam.getLimit() != 0) {
            PageHelper.startPage(baseParam.getPage(), baseParam.getLimit());
            PageInfo pageInfo = new PageInfo(marketGoods);
            long total = pageInfo.getTotal();
            // int pages = pageInfo.getPages();

            double i= Math.ceil(total / baseParam.getLimit());
            int pages= (int) i;



            int temp=1;
            int end=baseParam.getLimit()*baseParam.getPage();
            int start=end-baseParam.getLimit()+1;



            ArrayList<GoodsListVoOfGoods> goodsListVoOfGoods1 = new ArrayList<>();
            for (MarketGoods marketGood : marketGoods) {

                if (temp >= start && temp <= end) {
                    GoodsListVoOfGoods goodsListVoOfGoods = new GoodsListVoOfGoods(marketGood.getId(), marketGood.getGoodsSn(), marketGood.getName(), marketGood.getCategoryId(), marketGood.getBrandId(), marketGood.getGallery(), marketGood.getKeywords(), marketGood.getBrief(), marketGood.getIsOnSale(), marketGood.getSortOrder(), marketGood.getPicUrl(), marketGood.getIsNew(), marketGood.getIsHot(), marketGood.getUnit(), marketGood.getCounterPrice(), marketGood.getRetailPrice(), marketGood.getAddTime(), marketGood.getUpdateTime(), marketGood.getDeleted(), marketGood.getDetail());
                    goodsListVoOfGoods1.add(goodsListVoOfGoods);
                    goodsListVoData = new GoodsListVoData(total, pages, baseParam.getLimit(), baseParam.getPage(), goodsListVoOfGoods1);
                }
                temp++;

            }
        } else {
            PageInfo pageInfo = new PageInfo(marketGoods);
            long total = pageInfo.getTotal();
            ArrayList<GoodsListVoOfGoods> goodsListVoOfGoods1 = new ArrayList<>();
            for (MarketGoods marketGood : marketGoods) {
                // GoodsListVoOfGoodsOfUrl goodsListVoOfGoodsOfUrl = new GoodsListVoOfGoodsOfUrl(marketGood.getGallery().toString());
                // ArrayList<GoodsListVoOfGoodsOfUrl> goodsListVoOfGoodsOfUrls = new ArrayList<>();
                // goodsListVoOfGoodsOfUrls.add(goodsListVoOfGoodsOfUrl);

                GoodsListVoOfGoods goodsListVoOfGoods = new GoodsListVoOfGoods(marketGood.getId(), marketGood.getGoodsSn(), marketGood.getName(), marketGood.getCategoryId(), marketGood.getBrandId(), marketGood.getGallery(), marketGood.getKeywords(), marketGood.getBrief(), marketGood.getIsOnSale(), marketGood.getSortOrder(), marketGood.getPicUrl(), marketGood.getIsNew(), marketGood.getIsHot(), marketGood.getUnit(), marketGood.getCounterPrice(), marketGood.getRetailPrice(), marketGood.getAddTime(), marketGood.getUpdateTime(), marketGood.getDeleted(), marketGood.getDetail());
                goodsListVoOfGoods1.add(goodsListVoOfGoods);
            }
            goodsListVoData = new GoodsListVoData(total, 0, 0, 0, goodsListVoOfGoods1);
        }
        GoodsListVo goodsListVo = new GoodsListVo(0, goodsListVoData, "成功");
        return goodsListVo;
    }

    @Override
    public CatAndBrandVo queryCatAndBrand() {
        MarketCategoryExample marketCategoryExample = new MarketCategoryExample();


        MarketBrandExample marketBrandExample = new MarketBrandExample();

        MarketCategoryExample.Criteria criteria = marketCategoryExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        criteria.andLevelEqualTo("L1");
        List<MarketCategory> marketCategories = marketCategoryMapper.selectByExample(marketCategoryExample);
        // CatAndBrandVoOfData catAndBrandVoOfData1 = new CatAndBrandVoOfData();
        ArrayList<CatAndBrandOfDataOfCL> catAndBrandOfDataOfCLS = new ArrayList<>();
        ArrayList<CatAndBrandOfDataOfBL> catAndBrandOfDataOfBLS = new ArrayList<>();
        List<MarketBrand> marketBrands = marketBrandMapper.selectByExample(marketBrandExample);
        for (MarketCategory marketCategory : marketCategories) {
            MarketCategoryExample marketCategoryExample2 = new MarketCategoryExample();
            MarketCategoryExample.Criteria criteria1 = marketCategoryExample2.createCriteria();
            criteria1.andLevelEqualTo("L2");
            criteria1.andDeletedEqualTo(false);
            criteria1.andPidEqualTo(marketCategory.getId());
            List<MarketCategory> marketCategoriesByName = marketCategoryMapper.selectByExample(marketCategoryExample2);
            ArrayList<CatAndBrandOfDataOfCLOfChildren> catAndBrandOfDataOfCLOfChildrens = new ArrayList<>();
            for (MarketCategory category : marketCategoriesByName) {
                catAndBrandOfDataOfCLOfChildrens.add(new CatAndBrandOfDataOfCLOfChildren(category.getId(),category.getName()));
            }

            catAndBrandOfDataOfCLS.add(new CatAndBrandOfDataOfCL(marketCategory.getId(), marketCategory.getName(), catAndBrandOfDataOfCLOfChildrens));
        }



        for (MarketBrand marketBrand : marketBrands) {
            catAndBrandOfDataOfBLS.add(new CatAndBrandOfDataOfBL(marketBrand.getId(), marketBrand.getName()));
        }
        CatAndBrandVoOfData catAndBrandVoOfData = new CatAndBrandVoOfData(catAndBrandOfDataOfCLS, catAndBrandOfDataOfBLS);
        CatAndBrandVo catAndBrandVo = new CatAndBrandVo(0, catAndBrandVoOfData, "成功");
        return catAndBrandVo;
    }


    @Override
    public void insertGoods(GoodsCreateBo goodsCreateBo) {
        //插入商品
        MarketGoods marketGoods = new MarketGoods();
        marketGoods.setId(Integer.parseInt(goodsCreateBo.getGoods().getGoodsSn()));
        marketGoods.setGoodsSn(goodsCreateBo.getGoods().getGoodsSn());
        marketGoods.setName(goodsCreateBo.getGoods().getName());
        marketGoods.setCategoryId(goodsCreateBo.getGoods().getCategoryId());
        marketGoods.setBrandId(goodsCreateBo.getGoods().getBrandId());
        marketGoods.setGallery(goodsCreateBo.getGoods().getGallery());
        marketGoods.setKeywords(goodsCreateBo.getGoods().getKeywords());
        marketGoods.setBrief(goodsCreateBo.getGoods().getBrief());
        marketGoods.setIsOnSale(goodsCreateBo.getGoods().getIsOnSale());
        marketGoods.setSortOrder((short) 10);
        marketGoods.setPicUrl(goodsCreateBo.getGoods().getPicUrl());
        marketGoods.setShareUrl("");
        // marketGoods.setShareUrl(null);
        marketGoods.setIsNew(goodsCreateBo.getGoods().getIsNew());
        marketGoods.setIsHot(goodsCreateBo.getGoods().getIsHot());
        marketGoods.setUnit(goodsCreateBo.getGoods().getUnit());
        BigDecimal bigDecimal1 = new BigDecimal(goodsCreateBo.getGoods().getCounterPrice());
        marketGoods.setCounterPrice(bigDecimal1);
        //折扣价是-20之后的结果
        Integer num = Integer.parseInt(goodsCreateBo.getGoods().getCounterPrice());
        BigDecimal bigDecimal = new BigDecimal(num - 20);
        marketGoods.setRetailPrice(bigDecimal);
        marketGoods.setDetail(goodsCreateBo.getGoods().getDetail());
        Date date = new Date();
        // SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        // String nowTime = dateFormat.format( date );
        marketGoods.setAddTime(date);
        marketGoods.setUpdateTime(date);
        marketGoods.setDeleted(false);
        int insert = marketGoodsMapper.insertSelective(marketGoods);
        // int insert = marketGoodsMapper.insert(new MarketGoods());
        System.out.println("insertGoods = " + insert);
        //查询刚插入的商品的id
        Integer maxGoodsId = Integer.parseInt(goodsCreateBo.getGoods().getGoodsSn());
        //插入类目spec


        ArrayList<GoodsCreateSpecBo> specifications = goodsCreateBo.getSpecifications();
        for (GoodsCreateSpecBo specification : specifications) {
            MarketGoodsSpecification marketGoodsSpecification = new MarketGoodsSpecification();
            marketGoodsSpecification.setId(null);
            marketGoodsSpecification.setGoodsId(maxGoodsId);
            marketGoodsSpecification.setSpecification(specification.getSpecification());
            marketGoodsSpecification.setValue(specification.getValue());

            marketGoodsSpecification.setPicUrl(specification.getPicUrl());
            marketGoodsSpecification.setAddTime(date);
            marketGoodsSpecification.setUpdateTime(date);
            marketGoodsSpecification.setDeleted(false);
            int insertSpec = marketGoodsSpecificationMapper.insertSelective(marketGoodsSpecification);
            System.out.println("insertSpec = " + insertSpec);
        }

        //插入products
        ArrayList<GoodsCreateProductsBo> products = goodsCreateBo.getProducts();
        for (GoodsCreateProductsBo product : products) {
            MarketGoodsProduct marketGoodsProduct = new MarketGoodsProduct();
            marketGoodsProduct.setGoodsId(maxGoodsId);
            marketGoodsProduct.setSpecifications(product.getSpecifications());
            BigDecimal bigDecimal2 = new BigDecimal(product.getPrice());
            marketGoodsProduct.setPrice(bigDecimal2);

            marketGoodsProduct.setNumber(Integer.parseInt(product.getNumber()));
            marketGoodsProduct.setUrl(product.getUrl());
            marketGoodsProduct.setAddTime(date);
            marketGoodsProduct.setUpdateTime(date);
            marketGoodsProduct.setDeleted(false);
            int insertPro = marketGoodsProductMapper.insertSelective(marketGoodsProduct);
            System.out.println("insertPro = " + insertPro);
        }
        //插入attributes
        ArrayList<GoodsCreateAtrBo> attributes = goodsCreateBo.getAttributes();
        for (GoodsCreateAtrBo attribute : attributes) {
            MarketGoodsAttribute marketGoodsAttribute = new MarketGoodsAttribute();
            marketGoodsAttribute.setGoodsId(maxGoodsId);
            marketGoodsAttribute.setAttribute(attribute.getAttribute());
            marketGoodsAttribute.setValue(attribute.getValue());
            marketGoodsAttribute.setAddTime(date);
            marketGoodsAttribute.setUpdateTime(date);
            marketGoodsAttribute.setDeleted(false);
            int insertAttr = marketGoodsAttributeMapper.insertSelective(marketGoodsAttribute);
            System.out.println("insertAttr = " + insertAttr);
        }


    }

    @Override
    public GoodsDetailVo queryGoodsDetail(Integer id) {
        GoodsDetailVo goodsDetailVo = new GoodsDetailVo();
        MarketGoodsExample example = new MarketGoodsExample();
        MarketGoodsExample.Criteria criteria = example.createCriteria();
        criteria.andDeletedEqualTo(false);
        if (id != null && !id.equals("")) {
            criteria.andIdEqualTo(id);
        }
        List<MarketGoods> marketGoods = marketGoodsMapper.selectByExampleWithBLOBs(example);
        //创建Bo
        // ArrayList<Integer> goodsDetailOfCateVos = new ArrayList<>();
        GoodsDetailOfDataVo goodsDetailOfDataVo = new GoodsDetailOfDataVo();
        // Integer[] categoryIds = new Integer[2];
        List<Integer> categoryIds = new ArrayList<>();
        // ArrayList<Integer> categoryIds = goodsDetailOfDataVo.getCategoryIds();
        GoodsDetailOfGoodsVo goodsDetailOfGoodsVo = new GoodsDetailOfGoodsVo();
        ArrayList<GoodsCreateAtrBo> goodsCreateAtrBos = new ArrayList<>();
        ArrayList<GoodsDetailOfSpecVo> goodsDetailOfSpecVos = new ArrayList<>();
        ArrayList<GoodsDetailOfProVo> goodsDetailOfProVos = new ArrayList<>();
        if (marketGoods != null && marketGoods.size() == 1) {

            MarketGoods marketGoods1 = marketGoods.get(0);
            MarketGoodsAttributeExample attributeExample = new MarketGoodsAttributeExample();
            MarketGoodsAttributeExample.Criteria criteria1 = attributeExample.createCriteria();
            criteria1.andDeletedEqualTo(false);
            criteria1.andGoodsIdEqualTo(marketGoods1.getId());
            //查询Attri
            List<MarketGoodsAttribute> marketGoodsAttributes = marketGoodsAttributeMapper.selectByExample(attributeExample);
            for (MarketGoodsAttribute marketGoodsAttribute : marketGoodsAttributes) {
                goodsCreateAtrBos.add(new GoodsCreateAtrBo(marketGoodsAttribute.getAttribute(), marketGoodsAttribute.getValue()));
            }
            //查询goods
            goodsDetailOfGoodsVo.setId(marketGoods1.getId());
            goodsDetailOfGoodsVo.setGoodsSn(marketGoods1.getGoodsSn());
            goodsDetailOfGoodsVo.setName(marketGoods1.getName());
            goodsDetailOfGoodsVo.setCategoryId(marketGoods1.getCategoryId());
            goodsDetailOfGoodsVo.setBrandId(marketGoods1.getBrandId());
            // String gallery = marketGoods1.getGallery().toString();
            // String[] strings2 = null;
            //
            //
            // if (gallery != null) {
            //     try {
            //         strings2 = objectMapper.readValue(gallery, String[].class);
            //         String[] strings3 = new ArrayList<String>(Arrays.asList(strings2)).toArray(new String[0]);
            //         goodsDetailOfGoodsVo.setGallery(strings3);
            //     } catch (IOException e) {
            //         e.printStackTrace();
            //     }
            // } else {
            //     goodsDetailOfGoodsVo.setGallery(null);
            // }
            goodsDetailOfGoodsVo.setGallery(marketGoods1.getGallery());
            goodsDetailOfGoodsVo.setKeywords(marketGoods1.getKeywords());
            goodsDetailOfGoodsVo.setBrief(marketGoods1.getBrief());
            goodsDetailOfGoodsVo.setIsOnSale(marketGoods1.getIsOnSale());
            goodsDetailOfGoodsVo.setSortOrder((short) 10);
            goodsDetailOfGoodsVo.setPicUrl(marketGoods1.getPicUrl());
            goodsDetailOfGoodsVo.setShareUrl("");
            goodsDetailOfGoodsVo.setIsNew(marketGoods1.getIsNew());
            goodsDetailOfGoodsVo.setIsHot(marketGoods1.getIsHot());
            goodsDetailOfGoodsVo.setUnit(marketGoods1.getUnit());
            goodsDetailOfGoodsVo.setCounterPrice(marketGoods1.getCounterPrice());
            goodsDetailOfGoodsVo.setRetailPrice(marketGoods1.getRetailPrice());
            goodsDetailOfGoodsVo.setDetail(marketGoods1.getDetail());
            goodsDetailOfGoodsVo.setAddTime(marketGoods1.getAddTime());
            goodsDetailOfGoodsVo.setUpdateTime(marketGoods1.getUpdateTime());
            goodsDetailOfGoodsVo.setDeleted(marketGoods1.getDeleted());
            //添加category


            MarketCategory marketCategory = marketCategoryMapper.selectByPrimaryKey(marketGoods1.getCategoryId());
            if(marketCategory!=null){
                categoryIds.add(marketCategory.getPid());
                categoryIds.add(marketCategory.getId());
            }


            //查询spec
            MarketGoodsSpecificationExample marketGoodsSpecificationExample = new MarketGoodsSpecificationExample();
            MarketGoodsSpecificationExample.Criteria criteria3 = marketGoodsSpecificationExample.createCriteria();
            criteria3.andDeletedEqualTo(false);
            criteria3.andGoodsIdEqualTo(marketGoods1.getId());
            List<MarketGoodsSpecification> marketGoodsSpecifications = marketGoodsSpecificationMapper.selectByExample(marketGoodsSpecificationExample);
            for (MarketGoodsSpecification marketGoodsSpecification : marketGoodsSpecifications) {
                goodsDetailOfSpecVos.add(new GoodsDetailOfSpecVo(marketGoodsSpecification.getId(), marketGoodsSpecification.getGoodsId(), marketGoodsSpecification.getSpecification(), marketGoodsSpecification.getValue(), marketGoodsSpecification.getPicUrl(), marketGoodsSpecification.getAddTime(), marketGoodsSpecification.getUpdateTime(), marketGoodsSpecification.getDeleted()));
            }
            //查询products
            MarketGoodsProductExample marketGoodsProductExample = new MarketGoodsProductExample();
            MarketGoodsProductExample.Criteria criteria4 = marketGoodsProductExample.createCriteria();
            // criteria4.andDeletedEqualTo(false);
            criteria4.andGoodsIdEqualTo(id);
            List<MarketGoodsProduct> marketGoodsProducts = marketGoodsProductMapper.selectByExample(marketGoodsProductExample);

            for (MarketGoodsProduct marketGoodsProduct : marketGoodsProducts) {
                GoodsDetailOfProVo proVo = new GoodsDetailOfProVo();
                String[] specifications = marketGoodsProduct.getSpecifications();

                    proVo.setId(marketGoodsProduct.getId());
                    proVo.setGoodsId(marketGoodsProduct.getGoodsId());
                    // String[] strings1 = new String[1];
                    // try {
                    //     strings1 = objectMapper.readValue(specifications, String[].class);
                    // } catch (IOException e) {
                    //     e.printStackTrace();
                    // }
                    proVo.setSpecifications(specifications);
                    proVo.setPrice(marketGoodsProduct.getPrice());
                    proVo.setNumber(marketGoodsProduct.getNumber());
                    proVo.setUrl(marketGoodsProduct.getUrl());
                    proVo.setAddTime(marketGoodsProduct.getAddTime());
                    proVo.setUpdateTime(marketGoodsProduct.getUpdateTime());
                    proVo.setDeleted(marketGoodsProduct.getDeleted());
                    goodsDetailOfProVos.add(proVo);
            //     } else {
            //         goodsDetailOfProVos.add(new GoodsDetailOfProVo(marketGoodsProduct.getId(), marketGoodsProduct.getGoodsId(), null, marketGoodsProduct.getPrice(), marketGoodsProduct.getNumber(), marketGoodsProduct.getUrl(), marketGoodsProduct.getAddTime(), marketGoodsProduct.getUpdateTime(), marketGoodsProduct.getDeleted()));
            //     }
             }
            goodsDetailOfDataVo.setCategoryIds(categoryIds);
            goodsDetailOfDataVo.setAttributes(goodsCreateAtrBos);
            goodsDetailOfDataVo.setGoods(goodsDetailOfGoodsVo);
            goodsDetailOfDataVo.setProducts(goodsDetailOfProVos);
            goodsDetailOfDataVo.setSpecifications(goodsDetailOfSpecVos);


            goodsDetailVo = new GoodsDetailVo(0, goodsDetailOfDataVo, "成功");


        }

        return goodsDetailVo;

    }

    @Override
    public void updateGoods(GoodsUpdateBo goodsDetailVo) {
        //当前时间先赋值
        Date date = new Date();

        //商品更新
        GoodsUpdateOfGoodsBo goods = goodsDetailVo.getGoods();
        Integer id = goods.getId();


        MarketGoodsExample marketGoodsExample = new MarketGoodsExample();
        MarketGoodsExample.Criteria criteria = marketGoodsExample.createCriteria();
        criteria.andIdEqualTo(goods.getId());
        //有的列不更新
        MarketGoods marketGoods1 = marketGoodsMapper.selectByPrimaryKey(goods.getId());
        //赋值
        MarketGoods marketGoods = new MarketGoods();
        marketGoods.setId(goods.getId());
        marketGoods.setGoodsSn(goods.getGoodsSn());
        marketGoods.setName(goods.getName());
        marketGoods.setCategoryId(goods.getCategoryId());
        marketGoods.setBrandId(goods.getBrandId());
        marketGoods.setGallery(goods.getGallery());
        marketGoods.setKeywords(goods.getKeywords());
        marketGoods.setBrief(goods.getBrief());
        marketGoods.setIsOnSale(goods.getIsOnSale());
        marketGoods.setSortOrder(goods.getSortOrder());
        marketGoods.setPicUrl(goods.getPicUrl());
        marketGoods.setShareUrl(goods.getShareUrl());
        marketGoods.setIsNew(goods.getIsNew());
        marketGoods.setIsHot(goods.getIsHot());
        marketGoods.setUnit(goods.getUnit());
        marketGoods.setCounterPrice(new BigDecimal(goods.getCounterPrice()));
        marketGoods.setRetailPrice(goods.getRetailPrice());
        marketGoods.setAddTime(goods.getAddTime());
        marketGoods.setUpdateTime(date);
        marketGoods.setDeleted(goods.isDeleted());
        marketGoods.setDetail(goods.getDetail());

        System.out.println(goods.getId());
        marketGoodsMapper.updateByExampleWithBLOBs(marketGoods, marketGoodsExample);

        //products更新
        //直接定向更新


        ArrayList<GoodsDetailOfProVo> products = goodsDetailVo.getProducts();

        for (GoodsDetailOfProVo product : products) {
                MarketGoodsProduct goodsProduct = new MarketGoodsProduct();
            goodsProduct.setId(null);
            goodsProduct.setGoodsId(product.getGoodsId());
            goodsProduct.setSpecifications(product.getSpecifications());
            goodsProduct.setPrice(product.getPrice());
            goodsProduct.setNumber(product.getNumber());
            goodsProduct.setUrl(product.getUrl());
            goodsProduct.setAddTime(product.getAddTime());
            goodsProduct.setUpdateTime(date);
            goodsProduct.setDeleted(product.getDeleted());
            MarketGoodsProductExample marketGoodsProductExample1 = new MarketGoodsProductExample();
            marketGoodsProductExample1.createCriteria().andIdEqualTo(product.getId());
            marketGoodsProductMapper.updateByExampleSelective(goodsProduct,marketGoodsProductExample1);

        }

        //spec更新
        ArrayList<GoodsDetailOfSpecVo> specifications = goodsDetailVo.getSpecifications();
        for (GoodsDetailOfSpecVo specification : specifications) {
            MarketGoodsSpecification marketGoodsSpecification = new MarketGoodsSpecification(null, specification.getGoodsId(), specification.getSpecification(), specification.getValue(), specification.getPicUrl(),  specification.getAddTime(), date, specification.isDeleted());
            MarketGoodsSpecificationExample marketGoodsSpecificationExample = new MarketGoodsSpecificationExample();
            marketGoodsSpecificationExample.createCriteria().andIdEqualTo(specification.getId());
            marketGoodsSpecificationMapper.updateByExampleSelective(marketGoodsSpecification, marketGoodsSpecificationExample);
        }
        //attr更新  已修改!
        ArrayList<GoodsUpdateOfAtrBo> attributes = goodsDetailVo.getAttributes();
        MarketGoodsAttributeExample attributeExample = new MarketGoodsAttributeExample();
        MarketGoodsAttributeExample.Criteria criteria1 = attributeExample.createCriteria();
        criteria1.andGoodsIdEqualTo(goods.getId());
        //先删除再更新
        marketGoodsAttributeMapper.deleteByExample(attributeExample);
        for (GoodsUpdateOfAtrBo attribute : attributes) {
            if(!attribute.getDeleted()){
                MarketGoodsAttribute marketGoodsAttribute = new MarketGoodsAttribute();
                marketGoodsAttribute.setAttribute(attribute.getAttribute());
                marketGoodsAttribute.setId(null);
                marketGoodsAttribute.setGoodsId(id);
                marketGoodsAttribute.setAddTime(attribute.getUpdateTime());
                marketGoodsAttribute.setUpdateTime(attribute.getUpdateTime());
                marketGoodsAttribute.setValue(attribute.getValue());
                marketGoodsAttribute.setDeleted(attribute.getDeleted());
                marketGoodsAttribute.setUpdateTime(date);
                marketGoodsAttributeMapper.insert(marketGoodsAttribute);
            }

        }
    }

    @Override
    public GoodsListFromTopicVO selectbyId(Integer id) {
        MarketGoods marketGoods = marketGoodsMapper.selectByPrimaryKey(id);
        GoodsListFromTopicVO goodsListFromTopicVO = new GoodsListFromTopicVO();

        goodsListFromTopicVO.setId(marketGoods.getId());
        goodsListFromTopicVO.setName(marketGoods.getName());
        goodsListFromTopicVO.setBrief(marketGoods.getBrief());
        goodsListFromTopicVO.setPicUrl(marketGoods.getPicUrl());
        goodsListFromTopicVO.setIsNew(marketGoods.getIsNew());
        goodsListFromTopicVO.setIsHot(marketGoods.getIsHot());
        goodsListFromTopicVO.setCounterPrice(marketGoods.getCounterPrice());
        goodsListFromTopicVO.setRetailPrice(marketGoods.getRetailPrice());

        return goodsListFromTopicVO;
    }

    @Override
    public void deleteGoods(GoodsUpdateOfGoodsBo goodsdeleteOfGoodsBo) {

        MarketGoodsExample marketGoodsExample = new MarketGoodsExample();
        MarketGoodsExample.Criteria criteria = marketGoodsExample.createCriteria();
        criteria.andIdEqualTo(goodsdeleteOfGoodsBo.getId());
        MarketGoods marketGoods = marketGoodsMapper.selectByPrimaryKey(goodsdeleteOfGoodsBo.getId());
        marketGoods.setDeleted(true);

        marketGoodsMapper.updateByExampleWithBLOBs(marketGoods, marketGoodsExample);
    }
}
